package com.neuedu.shop.controller;


import com.neuedu.shop.entity.Category;
import com.neuedu.shop.service.CategoryService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Qin
 * @since 2021-04-10
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService service;

    /*@RequestMapping(value = "/categories",method = RequestMethod.GET)
    public String selectAll(ModelMap map){
        List<Category> categories = service.findToThree();
        map.addAttribute("categories",categories);
        return "forward:categories.jsp";
    }*/
    @ResponseBody
    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    public List<Category> selectAll(){
        List<Category> categories = service.findToThree();
        System.out.println("使用Ajax异步请求=======");
        return categories;
    }
    /*添加根类别*/
    @RequestMapping(value = "/insertRootCategory",method = RequestMethod.POST)
    public String insertRootCategory(String categoryName, String categoryDescription){
        service.insertRootCategory(categoryName,categoryDescription);
        return "redirect:categories.jsp";
    }
    @RequestMapping(value = "/insertChildCategory",method = RequestMethod.POST)
    public String insertChildCategory(String categoryName, String categoryDescription,Integer pid){
        System.out.println("Controller:"+categoryName+","+categoryDescription+","+pid);
        service.addChildCategory(categoryName,categoryDescription,pid);
        return "redirect:categories.jsp";
    }
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public String findById(Integer id, ModelMap map){
        Category category = service.selectById(id);
        map.addAttribute("category",category);
        return "forward:insert_child_category.jsp";
    }
}

